/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sip;

/**
 *
 * @author Akanksha Chauhan
 */
public class StartUpPage extends javax.swing.JFrame {
private SimpleInsulinPumpUI sipUi;
CheckStatus checkStatus = new CheckStatus();
InitSip initSip = new InitSip();
    
    /**
     * Creates new form StartUpPage
     */
    public StartUpPage() {
        initComponents();
        initSip.initialize();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        OnButton = new javax.swing.JButton();
        OffButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        OnButton.setLabel("ON");
        OnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OnButtonActionPerformed(evt);
            }
        });

        OffButton.setText("OFF");
        OffButton.setEnabled(false);
        OffButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OffButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(OnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OffButton, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(OffButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OnButtonActionPerformed
        /**
         * When ON button pressed, open the Insulin Pump GUI window
         */
        if(checkStatus.startupCheck()){
            SipLogger.Instance().debug("Start up Check was successfull");
            OnButton.setEnabled(false);
            OffButton.setEnabled(true);
        }
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                SimpleInsulinPumpUI sipUi = new SimpleInsulinPumpUI()
                if(checkStatus.startupCheck()){
                    //System.out.println("Start Up check was successfull");
                    sipUi = new SimpleInsulinPumpUI();
                    //System.out.println("sipUi has been called was successfull");
                    sipUi.setVisible(true);
                
                }
            }
        });
        
    }//GEN-LAST:event_OnButtonActionPerformed

    private void OffButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OffButtonActionPerformed
        /**
         * When Off button pressed, Insulin Pump GUI window should close
         */
        //System.out.println("I am in Off");
        initSip.writeCurrentValues();
        //System.out.println("passed write values");
        SipLogger.Instance().info("Saved Properties from Sip. Preparing for shutdown");
        SipLogger.Instance().info("Off Button pressed by user. Shutting down safely.");
        OffButton.setEnabled(false);
        OnButton.setEnabled(true);
        //System.out.println("passed enables");
 
        if(sipUi.checkInsulinThread.isAlive()){
            sipUi.checkInsulinRunnable.terminate();
            try{
                sipUi.checkInsulinThread.join();
                SipLogger.Instance().debug("CheckInsulinThred has been Stopped");
            }catch (InterruptedException e) { 
                    SipLogger.Instance().debug("CheckInsulinThred has been interrupted");       
                    }    
        }
        
        if(sipUi.checkBatteryThread.isAlive()){
            sipUi.checkBatteryRunnable.terminate();
            SipLogger.Instance().debug("CheckBatteryThred has been User Interrupted");
            try{
            sipUi.checkBatteryThread.join();
            SipLogger.Instance().debug("CheckBatteryThred has been Stopped");
            }catch (InterruptedException e) { 
                SipLogger.Instance().debug("CheckBatteryThred has been interrupted");       
                }    
        }
        
        if(sipUi.checkBubblesThread.isAlive()){
            sipUi.checkBubblesRunnable.terminate();
            SipLogger.Instance().debug("CheckBubblesThred has been User Interrupted");
            try{
            sipUi.checkBubblesThread.join();
            SipLogger.Instance().debug("CheckBubblesThred has been Stopped");
            }catch (InterruptedException e) { 
               SipLogger.Instance().debug("CheckBubblesThred has been interrupted");       
               }    
        }
        
        if(sipUi.basalThread != null){
            if(sipUi.basalThread.isAlive()){
                sipUi.basalInsulinRunnable.terminate();
                try{
                   sipUi.basalThread.join();
                   SipLogger.Instance().debug("basalThred has been Stopped");
                }catch (InterruptedException e) { 
                    SipLogger.Instance().debug("basalThred has been interrupted");       
                }    
            }
        }
        
        if(sipUi.bolusInsulinThread != null){
            if(sipUi.bolusInsulinThread.isAlive()){
                sipUi.bolusInsulinRunnable.terminate();
            try{
            sipUi.bolusInsulinThread.join();
            SipLogger.Instance().debug("bolusInsulinlThred has been Stopped");
            }catch (InterruptedException e) { 
                SipLogger.Instance().debug("bolusInsulinThred has been interrupted");       
                }    
            }
        }
   
        
        sipUi.dispose();
        SipLogger.Instance().debug("Off button worked");
   
        
    }//GEN-LAST:event_OffButtonActionPerformed

    /**
     * @param args the command line arguments
     */
     
        
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartUpPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartUpPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartUpPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartUpPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartUpPage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton OffButton;
    private javax.swing.JButton OnButton;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
